/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

#ifndef __RADAR_SIM_LAYER_MANAGER_H
#define __RADAR_SIM_LAYER_MANAGER_H

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <string>

#include "LayerManager.h"
#include "radarLayer.h"

/*<-------Defines--------->*/
/*<------End Defines------>*/

//------- Structs ---------//

class RadarSimManager : public LayerManager
{

	private:
		bool   playing;
		double timeScale;

		time_t time_offset;
		time_t min_time;
		time_t last_time;

		time_t user_min_time;

		GList         *radar_list;
		GooCanvasItem *site_group;
		GooCanvasItem *site_group_h;
		GooCanvasItem *img_group;
		GtkWidget     *radar_window;

		GooCanvasItem *scale_item;
		GooCanvasItem *scale_item_h;
		GooCanvasItem *currT_item;
		GooCanvasItem *uminT_item;
		GooCanvasItem *startT_text;
		GooCanvasItem *stopT_text;
		GooCanvasItem *currT_text;
		GooCanvasItem *uminT_text;

		void setup_scale(time_t startT, time_t userMinT, time_t currT, time_t stopT);

		NexradStations_t * findStation( GooCanvasItem *item );
		NexradStations_t * active_station;

		static RadarImageType_t const radarTypes[NUM_RADAR_TYPES];
		//bool getType[NUM_RADAR_TYPES];

		// -- http interface to radar maps
		static gboolean update_web_timeout(gpointer data);
		static gboolean update_image_timeout(gpointer data);
		static gboolean update_files_timeout(gpointer data);

		void one_time_init();
		void update_files();
		void update_images();

		GList         *radar_subs;

		GooCanvasItem *radar_layer;
		GHashTable    *tile_queue;

		void set_check_butons();
		void subscribeToRadar( uint32_t setTypes, uint32_t timeLoop );

		// contains flags indicating the various special characters
		// the uri string contains, that will be replaced when calculating
		// the uri to download.
		uint32_t inspect_map_uri(const gchar *uri);
		gchar   *replace_uri(const gchar *uri, const gchar *site, const gchar *type);

		RadarImgAlloc_t * addImage(string file, string gfw);

		String siteFile;

		bool minTimeSelected;
		double mouse_x;
		double mouse_y;


	public:
		RadarSimManager();
		~RadarSimManager();

		// layer interface
		virtual void init(GPSMap *map = NULL);
		virtual void updateMap();
		virtual void translate_from_zoomChange(double zoom_factor);
		virtual bool setShown(int setTo = -1);

		void update_display_time(int dir = 1, bool userStep = false);

		// canas setup
		void create_canvas();
		void setup_item(GooCanvasItem *item);

		// signals
		static bool item_key_event_cb(GooCanvasItem *item, GooCanvasItem *target_item, GdkEventKey *event, RadarSimManager *me);
		static bool item_button_press_event_cb(GooCanvasItem *item, GooCanvasItem *target_item, GdkEventButton *event, RadarSimManager *me);
		static bool item_button_release_event_cb(GooCanvasItem *item, GooCanvasItem *target_item, GdkEventButton *event, RadarSimManager *me);
		static bool item_motion_notify_event_cb(GooCanvasItem *item, GooCanvasItem *target_item, GdkEventMotion *event, RadarSimManager *me);
		static bool on_focus_in(GooCanvasItem *item, GooCanvasItem *target, GdkEventFocus *event, gpointer data);
		static bool on_focus_out(GooCanvasItem *item, GooCanvasItem *target, GdkEventFocus *event, gpointer data);

		// media signals
		static bool radar_media_button_cb(GooCanvasItem *item, GooCanvasItem *target_item, GdkEventButton *event, RadarSimManager *me);

		// radar control window
		void create_radar_window();
		static void radar_button_clicked (GtkWidget *w, gpointer user_data);
		static void radar_cancel_clicked(GtkWidget *w, gpointer user_data);
		static gboolean radar_window_destroy(GtkWidget * widget, GdkEventKey * event, gpointer user_data);

		bool is_running() { return playing; }
		void step(int dir=1) {
			if( !playing ) update_display_time(dir,true);
		}
		void step_runRate(int dir=1) {
			if( dir > 0) timeScale *= 2.0;
			else if( dir < 0) timeScale /= 2.0;
		}	
		void minT_step(int dir=1) {
			//cout << "minT_step" << endl;
			if(dir == 0) {
				user_min_time = min_time;
			} else {
				user_min_time += (int)(dir * timeScale);
				if( user_min_time > last_time )
					user_min_time = last_time;		 
				else if( user_min_time < min_time )
					user_min_time = min_time;
			}
		}
		void toggle_running(int val=-1) {
			if( val < 0 ) playing ^= 1;
			else playing = val;
		}
};

#endif
